﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Drawing;
using System.Data.SqlClient;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class WebUserControl_ControlQuanLyMatKhauBA : System.Web.UI.UserControl
{
    ClassExecute.ClassExecute dt;
    string str = BCDataCenter.Configuration.CurrentConnectionString;
    static string error = "";
    static bool flag = false;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void ImgSave_Click(object sender, ImageClickEventArgs e)
    {
        try
        {
            if (flag)
            {
                dt = new ClassExecute.ClassExecute();
                bool kq = dt.ExecuteSQl_bool(str, CommandType.StoredProcedure, "K_UpdatePassTVV", ref error,
                    new SqlParameter("@salesp_key", txtMaTVV.Text),
                    new SqlParameter("@NewPass", txtNewPass.Text));
                if (kq)
                {
                    uscMsgBox1.AddMessage("Cập nhật mật khẩu thành công", uscMsgBox.enmMessageType.Success);
                   
                }
                else
                {
                    uscMsgBox1.AddMessage("Cập nhật mật khẩu thất bại", uscMsgBox.enmMessageType.Success);

                }
               
            }
            else
            {
                uscMsgBox1.AddMessage("Dữ liệu không đúng", uscMsgBox.enmMessageType.Success);

            }
            unLock();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void ImgFind_Click(object sender, ImageClickEventArgs e)
    {
        TabContainer1.ActiveTabIndex = 1;
    }
    void Lock()
    {
        txtMaTVV.ReadOnly = true;
        txtMaTVV.BackColor = Color.Yellow;
      
        flag = true;
    }
    protected void ISearch_Click(object sender, ImageClickEventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "K_FindTVVPass", CommandType.StoredProcedure,
            new SqlParameter("@para", txtInfo.Text));
        if (db.Rows.Count == 1)
        {
            txtMaTVV.Text = db.Rows[0]["salesp_key"].ToString();
            txtHoTen.Text = db.Rows[0]["salesp_name"].ToString();
            txtOldPass.Text = db.Rows[0]["PW"].ToString();
            txtTTPP.Text = db.Rows[0]["sitename"].ToString();
            Lock();
            TabContainer1.ActiveTabIndex = 0;
        }
        else if (db.Rows.Count > 1)
        {
            GridView2.DataSource = db;
            GridView2.DataBind();
        }
        else
        {
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
    }
    protected void txtMaTVV_TextChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();

        DataTable db = dt.Get_Data_DataTable(str, ref error, "K_GetTVVPass", CommandType.StoredProcedure,
            new SqlParameter("@salesp_key",txtMaTVV.Text));
        if (db.Rows.Count == 1)
        {
            txtMaTVV.Text = db.Rows[0]["salesp_key"].ToString();
            txtHoTen.Text = db.Rows[0]["salesp_name"].ToString();
            txtOldPass.Text = db.Rows[0]["PW"].ToString();
            txtTTPP.Text = db.Rows[0]["sitename"].ToString();
            Lock();
            TabContainer1.ActiveTabIndex = 0;
        }
    }
    protected void txtInfo_TextChanged(object sender, EventArgs e)
    {
        dt = new ClassExecute.ClassExecute();
        DataTable db = dt.Get_Data_DataTable(str, ref error, "K_FindTVVPass", CommandType.StoredProcedure,
            new SqlParameter("@para", txtInfo.Text));
        if (db.Rows.Count == 1)
        {
            txtMaTVV.Text = db.Rows[0]["salesp_key"].ToString();
            txtHoTen.Text = db.Rows[0]["salesp_name"].ToString();
            txtOldPass.Text = db.Rows[0]["PW"].ToString();
            txtTTPP.Text = db.Rows[0]["sitename"].ToString();
            Lock();
            TabContainer1.ActiveTabIndex = 0;
        }
        else if (db.Rows.Count > 1)
        {
            GridView2.DataSource = db;
            GridView2.DataBind();
        }
        else
        {
            GridView2.DataSource = null;
            GridView2.DataBind();
        }
    }
    protected void GridView2_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            GridView2.PageIndex = e.NewPageIndex;
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "K_FindTVVPass", CommandType.StoredProcedure,
                new SqlParameter("@para", txtInfo.Text));
            GridView2.DataSource = db;
            GridView2.DataBind();
        }
        catch (Exception ex)
        {
            BCDataCenter.ClassWriteLogErr obj = new BCDataCenter.ClassWriteLogErr();
            string method = System.Reflection.MethodBase.GetCurrentMethod().Name;
            obj.ErrorLog(ex.Message, method);
        }
    }
    protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
    {

        GridViewRow grv = ((Control)e.CommandSource).NamingContainer as GridViewRow;
        if (e.CommandName == "chon")
        {
            dt = new ClassExecute.ClassExecute();
            DataTable db = dt.Get_Data_DataTable(str, ref error, "K_GetTVVPass", CommandType.StoredProcedure,
                new SqlParameter("@salesp_key", grv.Cells[0].Text));
            txtMaTVV.Text = db.Rows[0]["salesp_key"].ToString();
            txtHoTen.Text = db.Rows[0]["salesp_name"].ToString();
            txtOldPass.Text = db.Rows[0]["PW"].ToString();
            txtTTPP.Text = db.Rows[0]["sitename"].ToString();
            Lock();
            TabContainer1.ActiveTabIndex = 0;
        }
    }
    protected void ImgNew_Click(object sender, ImageClickEventArgs e)
    {
        unLock();
    }
    void unLock()
    {
        txtMaTVV.ReadOnly = false;
        txtMaTVV.BackColor = Color.White;
        txtMaTVV.Text = "";
        txtHoTen.Text = "";
        txtTTPP.Text = "";
        txtOldPass.Text = "";
        txtNewPass.Text = "";
        txtNewPassRetype.Text = "";
        flag = false;
    }
}
